package com.fin.quant.domain.repository.impl;

import com.fin.quant.domain.entity.AiConversationsEntity;

import com.fin.quant.domain.mapstruct.AiConversationsMapperStruct;
import com.fin.quant.domain.repository.ConversationRepository;
import com.fin.quant.instra.mysql.mapper.AiConversationsMapper;
import com.fin.quant.instra.mysql.mapper.AiMessagesMapper;
import com.fin.quant.instra.mysql.model.AiConversations;
import com.fin.quant.instra.mysql.model.AiConversationsExample;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * AI对话服务
 */
@Service
@Transactional
@Slf4j
public class ConversationRepositoryImpl implements ConversationRepository {

    @Autowired
    private AiConversationsMapper aiConversationsMapper;

    @Override
    public AiConversationsEntity saveOrUpdate(AiConversationsEntity entity) {
        if (entity == null) {
            throw new IllegalArgumentException("Entity cannot be null");
        }

        AiConversations model = AiConversationsMapperStruct.INSTANCE.entity2Model(entity);

        if (entity.getId() == null) {
            // 新增记录
            aiConversationsMapper.insertSelective(model);
        } else {
            // 更新记录
            aiConversationsMapper.updateByPrimaryKeySelective(model);
        }

        // 将更新后的model转换回entity并返回
        return AiConversationsMapperStruct.INSTANCE.model2Entity(model);
    }

    @Override
    public Optional<AiConversationsEntity> findByConversationId(String conversationId) {
        AiConversationsExample example = new AiConversationsExample();
        example.createCriteria().andConversationIdEqualTo(conversationId);
        List<AiConversations> aiConversations = aiConversationsMapper.selectByExample(example);
        return CollectionUtils.isNotEmpty(aiConversations) ?
                Optional.of(AiConversationsMapperStruct.INSTANCE.model2Entity(aiConversations.get(0)))
                : Optional.empty();
    }


    @Override
    public List<AiConversationsEntity> findByUserIdOrderByUpdatedAtDesc(Long userId) {
        AiConversationsExample example = new AiConversationsExample();
        example.createCriteria().andUserIdEqualTo(userId);
        List<AiConversations> aiConversations = aiConversationsMapper.selectByExample(example);
        return aiConversations.stream().map(AiConversationsMapperStruct.INSTANCE::model2Entity).collect(Collectors.toList());
    }

    @Override
    public List<AiConversationsEntity> findByPageIdOrderByUpdatedAtDesc(String pageId) {
        AiConversationsExample example = new AiConversationsExample();
        example.createCriteria().andPageIdEqualTo(pageId);
        List<AiConversations> aiConversations = aiConversationsMapper.selectByExample(example);
        return aiConversations.stream().map(AiConversationsMapperStruct.INSTANCE::model2Entity).collect(Collectors.toList());
    }


}
